#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"LinkTree.h"
#define true 1
#define false 0
struct student
{
    int id;
    char* name;
    int score;
};
struct student* CreateStudent(int id, char* name, int score)
{
    struct student* stu = (struct student*)malloc(sizeof(struct student));
    stu->id = id;
    stu->name = (char*)name;
    stu->score = score;
    return stu;
}
void FreeStu(struct student* stu)
{
    if (stu == NULL)
    {
        return;
    }
    free(stu);
}

void Print(ElementType data)
{
    if (data.type == structType)
    {
        if (data.value != NULL)
        {
            struct student* stu = (struct student*)data.value;
            printf("name : %s , id : %d , score : %d \n", stu->name, stu->id, stu->score);
        } 
    }
    else if (data.type == charType)
    {
        char* temp = (char*)data.value;
        printf("%s\n", temp);
    }
    return;
}

int IsEqual(ElementType data1, ElementType data2)
{
    if (data1.type == structType && data2.type == structType)
    {
        struct student* stu = (struct student*)data1.value;
        struct student* stu2 = (struct student*)data2.value;
        if (stu->id == stu2->id)
        {
            return true;
        }
        return false;
    }
    else if(data1.type == charType && data2.type == charType)
    {
        char* temp1 = (char*)data1.value;
        char* temp2 = (char*)data2.value;
        if (strcmp(temp1, temp2) == 0)
            return true;
        return false;
    }
}
int main()
{
    LTree tree;
    InitTree(&tree);

    struct UniversalType typevalue;
    typevalue.value = "class1";
    typevalue.type = charType;
    LTNode* node1 = CreateTreeNode(typevalue);

    typevalue.value = CreateStudent(1, "zhangsan", 100);
    typevalue.type = structType;
    LTNode* node2 = CreateTreeNode(typevalue);

    typevalue.value = CreateStudent(2, "lisi", 90);
    typevalue.type = structType;
    LTNode* node3 = CreateTreeNode(typevalue);

    ConnectBranch(node1, node2);
    ConnectBranch(node1, node3);
    ConnectBranch(tree.root,node1);
    TravelTree(&tree, Print);
    typevalue.type = charType;
    typevalue.value = "class1";
    LTNode* findnode = FindTreeNode(&tree, typevalue, IsEqual);
    if (findnode != NULL)
    {
        char* className = "class2";
        findnode->data.value = className;
    }

    TravelTree(&tree, Print);

    typevalue.type = structType;
    typevalue.value = CreateStudent(1, "", 0);
    findnode = FindTreeNode(&tree, typevalue, IsEqual);
    if (findnode != NULL)
    {
        struct student* stu = (struct student*)findnode->data.value;
        stu->score = 60;
    }
    free(typevalue.value);
    TravelTree(&tree, Print);
}